package com.itheima.api;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.api.UserApi;
import com.itheima.domain.User;
import com.itheima.mapper.UserMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.http.ResponseEntity;

import javax.annotation.Resource;
import java.util.List;

@DubboService
public class UserApiImpl implements UserApi {

    @Resource
    private UserMapper userMapper;

    /**
     * 根据手机号查询用户
     * @param mobile
     * @return
     */
    @Override
    public User findByMobile(String mobile) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getMobile,mobile);
        return  userMapper.selectOne(queryWrapper);
    }

    /**
     * 保存用户数据
     * @param user
     */
    @Override
    public Long save(User user) {
        userMapper.insert(user);
        return user.getId();
    }

    /**
     * 修改手机号
     * @param
     */
    @Override
    public void update(Long id,String mobile) {
        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(User::getId,id);
        updateWrapper.set(User::getMobile,mobile);
        userMapper.update(null,updateWrapper);
    }

    /**
     * 修改用户信息
     * @param user
     */
    @Override
    public void update(User user) {
        userMapper.updateById(user);
    }

    /**
     * 选出没有注册环信的user
     * @return
     */
    @Override
    public List<User> findAll() {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.isNull(User::getHxUser);
        return userMapper.selectList(queryWrapper);
    }

    /**
     * 根据环信id查用户
     * @param hxId
     * @return
     */
    @Override
    public User getByHxId(String hxId) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getHxUser,hxId);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public User findById(long userId) {
        return userMapper.selectById(userId);
    }


}
